<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户订单号" prop="merchantOrderNo" label-width="100px">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户订单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单状态" prop="reportStatus">
        <el-select v-model="queryParams.merchantStatus" placeholder="请选择订单状态"
                   filterable clearable size="small"
        >
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.app_merchant_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户名称" prop="tenantId">
        <el-select v-model="queryParams.tenantId" @change="chooseTenant" filterable clearable placeholder="请输入商户名称"
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="tenant in tenants" :key="parseInt(tenant.id)" :label="tenant.name"
                     :value="parseInt(tenant.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分公司名称" prop="subTenantId" label-width="100px">
        <el-select v-model="queryParams.subTenantId" @change="chooseSubTenant" filterable clearable
                   placeholder="请输入分公司名称"
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="subTenant in subTenants" :key="parseInt(subTenant.id)" :label="subTenant.name"
                     :value="parseInt(subTenant.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="shopId">
        <el-select v-model="queryParams.shopId" filterable clearable placeholder="请输入门店名称"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="shop in shops" :key="parseInt(shop.id)" :label="shop.name"
                     :value="parseInt(shop.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="员工名称" prop="shopUserName">
        <el-input v-model="queryParams.shopUserName" placeholder="请输入员工名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单时间" prop="merchantOrderTime">
        <el-date-picker v-model="queryParams.merchantOrderTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="完成时间" prop="completionTime">
        <el-date-picker v-model="queryParams.completionTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
      <!--                   v-hasPermi="['pekingfish:report-forms-detail:create']">新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:report-forms-detail:export']">导出汇总
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="title">汇总</div>
    <el-table :data="collectList" v-loading="loading" max-height="350" style="width: 100%">
      <el-table-column label="报价单量" align="center" prop="bjdl"/>
      <el-table-column label="成交单量" align="center" prop="cjdl"/>
      <el-table-column label="差错单量" align="center" prop="ccdl"/>
      <el-table-column label="退款退货单量" align="center" prop="thtkl"/>
      <el-table-column label="成交金额" align="center" prop="cjje"/>
      <el-table-column label="扣款金额" align="center" prop="kkje"/>
      <el-table-column label="退款金额" align="center" prop="tkje"/>
      <el-table-column label="分润金额" align="center" prop="frje"/>
      <el-table-column label="回收积分" align="center" prop="hsjf"/>
      <el-table-column label="成交率" align="center" prop="cjl"/>
      <el-table-column label="扣款率" align="center" prop="kkl"/>
      <el-table-column label="退货率" align="center" prop="thl"/>
    </el-table>
    <div class="title" style="margin-top: 10px">明细</div>
    <el-col :span="1.5">
      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExportDetail"
                 :loading="exportDetailLoading"
                 v-hasPermi="['pekingfish:report-forms-detail:export']">导出明细
      </el-button>
    </el-col>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="商户订单号" align="center" prop="merchantOrderNo" width="150" fixed="left"/>
      <el-table-column label="订单状态" align="center" prop="merchantStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_merchant_status" :value="scope.row.merchantStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="手机品牌" align="center" prop="brand"/>
      <el-table-column label="手机型号" align="center" prop="model"/>
      <el-table-column label="商户类型" align="center" prop="accountType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.app_account_type" :value="scope.row.tenantType"/>
        </template>
      </el-table-column>
      <el-table-column label="店员名称" align="center" prop="shopUserName"/>
      <el-table-column label="门店名称" align="center" prop="shopName"/>
      <el-table-column label="分公司名称" align="center" prop="subTenantName"/>
      <el-table-column label="商户名称" align="center" prop="tenantName"/>
      <el-table-column label="平台报价" align="center" prop="offerPrice"/>
      <el-table-column label="用户改价" align="center" prop="changePrice"/>
      <el-table-column label="回收价" align="center" prop="recovery"/>
      <el-table-column label="扣款金额" align="center" prop="buckleMoney"/>
      <el-table-column label="扣除积分" align="center" prop="deductPoints"/>
      <el-table-column label="最终回收价" align="center" prop="ultimatelyRecovery"/>
      <el-table-column label="店员本单分润" align="center" prop="shopUserPrice"/>
      <el-table-column label="店长本单分润" align="center" prop="shopManagerPrice"/>
      <el-table-column label="分公司本单分润" align="center" prop="subTenantPrice"/>
      <el-table-column label="商户本单分润" align="center" prop="tenantPrice"/>
<!--      <el-table-column label="店员可用分润" align="center" prop="shopUserPrices"/>-->
<!--      <el-table-column label="店长可用分润" align="center" prop="shopManagerPrices"/>-->
<!--      <el-table-column label="分公司可用分润" align="center" prop="subTenantPrices"/>-->
<!--      <el-table-column label="商户可用分润" align="center" prop="tenantPrices"/>-->
      <el-table-column label="本单分润" align="center" prop="priceTotal"/>
      <el-table-column label="店员本单积分" align="center" prop="shopUserIntegral"/>
      <el-table-column label="店长本单积分" align="center" prop="shopManagerIntegral"/>
      <el-table-column label="分公司本单积分" align="center" prop="subTenantIntegral"/>
      <el-table-column label="商户本单积分" align="center" prop="tenantIntegral"/>
      <el-table-column label="本单积分" align="center" prop="integralTotal"/>
      <el-table-column label="订单时间" align="center" prop="merchantOrderTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.merchantOrderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款时间" align="center" prop="payTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扣款时间" align="center" prop="buckleTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.buckleTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completionTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.completionTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  getReportFormsDetailPage,
  exportReportFormsDetailExcel, getCollectPage, exportReportFormsDetailExcelDetail
} from "@/api/pekingfish/reportFormsDetail";
import {getBranchCompanyByTenantId} from "@/api/merchantManagement/branchCompany";
import {getStoreBySuTenantId} from "@/api/merchantManagement/store";
import {getBusinessList} from "@/api/merchantManagement/business";

export default {
  name: "ReportFormsDetail",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      collectLoading: true,
      // 导出遮罩层
      exportLoading: false,

      // 导出遮罩层
      exportDetailLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账单列表
      list: [],
      // 商户名称
      tenants: [],
      // 分公司名称
      subTenants: [],
      shops: [],
      collectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        merchantOrderNo: null,
        merchantStatus: null,
        completionTime: [],
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getCollectList()
    // 商户名称
    getBusinessList().then(response => {
      this.tenants = response.data.list
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getReportFormsDetailPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    chooseTenant(tenantId) {
      console.log(tenantId + '获取的id')
      // 分公司名称
      getBranchCompanyByTenantId(tenantId).then(response => {
        this.subTenants = response.data
      })
    },
    chooseSubTenant(subTenantId) {
      console.log(subTenantId + '获取的id')
      // 门店名称
      getStoreBySuTenantId(subTenantId).then(response => {
        this.shops = response.data
      })
    },
    getCollectList() {
      this.collectLoading = true;
      // 执行查询
      getCollectPage(this.queryParams).then(response => {
        this.collectList = [];
        this.collectList.push(response.data);
        this.total = 1;
        this.collectLoading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        merchantOrderNo: undefined,
        completionTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
      this.getCollectList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有对账单数据项?').then(() => {
        this.exportLoading = true;
        return exportReportFormsDetailExcel(params);
      }).then(response => {
        this.$download.excel(response, '对账单汇总.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    },

    /** 导出按钮操作 */
    handleExportDetail() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有对账单明细数据项?').then(() => {
        this.exportDetailLoading = true;
        return exportReportFormsDetailExcelDetail(params);
      }).then(response => {
        this.$download.excel(response, '对账单明细.xls');
        this.exportDetailLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
<style>
.title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
}
</style>
